<?php
/**
 * regd by PhpStorm.
 * User: lee
 */

namespace Biz\validate\activity;

use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\model\activity\CouponConfig as mCouponConfig;

class Coupon extends \Doraemon\pockets\validate\Base
{

    public static function rules()
    {

        //店铺专属券的范围列表
        $goodsRangeList = array_keys(mCouponConfig::$goodsRangeDesc);

        //渠道范围
        $channelList = array_keys(mCouponConfig::$channelDesc);

        return [
            ['title', 'required', null, '请输入优惠券标题', ['create']],
            ['title', 'length', '1,15', '优惠券标题最大15个字', ['create']],

            ['channel', 'required', null, '请选择发放渠道', ['create']],
            ['channel', 'InList', ['list'=>$channelList], '请正确选择发放渠道', ['create']],

            ['start_time', 'required', null, '请正确输入开始时间', ['create']],
            ['start_time', 'callback', ['callback' => [new self(), 'checkTimeValid']], '开始时间必须大于当前时间并且小于结束时间', ['create']],

            ['stop_time', 'required', null, '请正确输入结束时间', ['create']],

            ['rule_config', 'callback', ['callback' => [new self(), 'checkRuleConfig']], '请正确输入规则', ['create']],

            ['goods_range', 'required', null, '请正确选择商品范围', ['create']],
            ['goods_range', 'InList', ['list'=>$goodsRangeList], '请正确选择商品范围', ['create']],

            ['include_goods_ids','callback',['callback'=>[new self(),'checkCreateIncludeGoodsIds']],'请正确填写商品支持范围',['create']],



            ['total_count', 'required', null, '请正确输入发行量', ['update']],
            ['total_count', 'between', sprintf('0,%d',mCouponConfig::MAX_TOTAL_COUNT), '发行量最多100万', ['update']],
            ['total_count', 'callback', ['callback' => [new self(), 'checkEditTotalCount']], '在编辑模式，发行量必须增加不能减少', ['update']],

            ['receive_rule', 'required', null, '请正确填写限领张数', ['update']],
            ['receive_rule', 'between', sprintf('0,%d',mCouponConfig::MAX_RECEIVE_PER_LIMIT), sprintf('请正确填写限领张数，最多%d张',mCouponConfig::MAX_RECEIVE_PER_LIMIT), ['update']],

            ['incre_goods_ids','callback',['callback'=>[new self(),'checkEditIncreGoodsIds']],'请正确填写商品支持范围',['update']]

        ];

    }

    public function checkTimeValid($value, $valueIdentifier, $allData)
    {

        $nowTime = strtotime(date('Y-m-d'));

        if (strtotime($value) < $nowTime) {
            return false;
        }

        if (strtotime($value) > strtotime($allData['stop_time'])) {
            return false;
        }

        return true;
    }

    public function checkRuleConfig($value, $valueIdentifier, $allData, $obj)
    {

        $value = json_decode($value, true);

        if (empty($value) && !is_array($value)) {
            return false;
        }

        foreach ($value as $rule) {

            //满金额和减金额必须为大于0的整数
            if ($rule['detract_amount'] < 0 || $rule['full_amount'] < 0) {

                $obj->setMessageTemplate("满金额或减金额不能小于0");

                return false;
            }

            //满金额和减金额必须是整数
            if (!is_numeric($rule['detract_amount']) || !is_numeric($rule['full_amount'])) {

                $obj->setMessageTemplate("满金额或减金额必须为整数");

                return false;
            }

            //满金额不可小于减金额
            if ($rule['full_amount'] < $rule['detract_amount']) {

                $obj->setMessageTemplate("满金额必须大于减金额");

                return false;
            }

            if($rule['total_count']<=0){

                $obj->setMessageTemplate("发行量必须大于1个");

                return false;
            }

            if($rule['total_count'] > mCouponConfig::MAX_TOTAL_COUNT){

                $obj->setMessageTemplate(sprintf("发行量必须小于%d",mCouponConfig::MAX_TOTAL_COUNT));

                return false;
            }

            if($rule['receive_rule']<0 || $rule['receive_rule']>mCouponConfig::MAX_RECEIVE_PER_LIMIT){

                $obj->setMessageTemplate("限领个数必须是1-5，或者不限");

                return false;
            }

        }

        return true;

    }


    public function checkCreateIncludeGoodsIds($value, $valueIdentifier, $allData,$obj){
        if($allData['goods_range'] == mCouponConfig::GOODS_RANGE_PART){
            if(empty($value)){
                return false;
            }

            $goodsIds = explode(',',$value);

            $count = count($goodsIds);

            if($count > mCouponConfig::MAX_LIMIT_GOODS_NUM_PER){
                $obj->setMessageTemplate(sprintf('最多可选中%d件商品',mCouponConfig::MAX_LIMIT_GOODS_NUM_PER));

                return false;
            }

            $goodsCount =mGoodsBasicInfo::count(['shop_id'=>$allData['shop_id'],'goods_id'=>$goodsIds]);

            if($count != $goodsCount){

                $obj->setMessageTemplate('部分商品不在商品库中');

                return false;
            }
        }

        return true;
    }

    public function checkEditIncreGoodsIds($value, $valueIdentifier, $allData,$obj){

        if(empty($value)){
            return true;
        }

        $couponInfo = mCouponConfig::getCouponInfo($allData['coupon_id']);

        if($couponInfo['goods_range'] == mCouponConfig::GOODS_RANGE_PART){

            //统计该优惠券已添加的商品使用范围
            $includeGoodsIds = explode(',',$couponInfo['include_goods_ids']);
            $includeGoodsCount = count($includeGoodsIds);

            //在编辑模式下，增量的商品使用范围
            $increGoodsIds = explode(',',$value);
            $increGoodsIdsCount = count($increGoodsIds);


            //判断此次添加的商品是否在列表中已存在
            if(array_intersect($includeGoodsIds,$increGoodsIds)){

                $obj->setMessageTemplate('此次新增的商品在列表中已存在');

                return false;
            }

            //判断是否满足商品数限定
            if(($includeGoodsCount+$increGoodsIdsCount) > mCouponConfig::MAX_LIMIT_GOODS_NUM_PER){

                $obj->setMessageTemplate(sprintf('最多可选中%d件商品',mCouponConfig::MAX_LIMIT_GOODS_NUM_PER));

                return false;
            }


            //此次增加的商品是否有效
            $goodsCount =mGoodsBasicInfo::count(['shop_id'=>$allData['shop_id'],'goods_id'=>$increGoodsIds]);

            if($increGoodsIdsCount != $goodsCount){
                $obj->setMessageTemplate('部分商品不在商品库中');

                return false;
            }
        }

        return true;
    }


    public function checkEditTotalCount($value, $valueIdentifier, $allData){
        $couponInfo = mCouponConfig::getCouponInfo($allData['coupon_id']);

        if($value < $couponInfo['total_count'] ){
            return false;
        }

        return true;



    }



}